/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.DiscountCategoryReportModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;

/**
 *  
 */
public class DiscountCategoriesReportComponent extends EditTableComponent
{	
	protected List<DiscountCategoryReportModel> discountCategoryReportModels;

	/**
	 * Constructor
	 */
	public DiscountCategoriesReportComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		editable = false;
		discountCategoryReportModels = new ArrayList<DiscountCategoryReportModel>();
		paginate = true;
		tableType = "discount categories";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		discountCategoryReportModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		DiscountCategoryReportModel model = new DiscountCategoryReportModel(jsonObject);
		discountCategoryReportModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = discountCategoryReportModels.size();
		numberOfTableColumns = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Discount Category");
		grid.setText(0, 1, "Product Name");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, discountCategoryReportModels.get(entryIndex));
	}

	public void displayRow(Grid grid, int row, DiscountCategoryReportModel model) {
		grid.setText(row, 0, model.getName());
		InlineLabel nameLabel = new InlineLabel(model.getProductName());
		nameLabel.addClickHandler(jumpClickHandler);
		nameLabel.addStyleName("ComponentLink");
		nameLabel.addStyleName("index:"+(row-1));
		grid.setWidget(row, 1, nameLabel);
	}

	public List<DiscountCategoryReportModel> getDiscountCategoryReportModels() {
		return discountCategoryReportModels;
	}

	protected ClickHandler jumpClickHandler = new ClickHandler() {
		public void onClick(ClickEvent event) {
			Widget source = (Widget)event.getSource();
			String stylenames = source.getStyleName();
			int loc = stylenames.indexOf("index:");
			stylenames = stylenames.substring(loc+6);
			loc = stylenames.indexOf(" ");
			if (loc > -1) stylenames = stylenames.substring(0, loc);
			int index = Integer.parseInt(stylenames);
			DiscountCategoryReportModel model = discountCategoryReportModels.get(index);
			String history = "EditProductComponent--1--" + model.getCategoryId() + "--" + model.getProductId();
			onSelection(history);
		}
	};
}
